﻿<?php
// //require_once('../../config.php');
// global $CFG;
GLOBAL $CFG;
//$CFG->unicodedb = true;

require_once($CFG->libdir . '/adminlib.php');
require_once($CFG->libdir . '/accesslib.php'); //dùng hàm has_capability
require_once($CFG->dirroot . '/lib/moodlelib.php');	//Sử dụng hàm getdate để lấy dữ liệu ngày tháng
require_once($CFG->dirroot.'/course/lib.php');			//Sử dụng hàm create_course để mở course
require_once($CFG->dirroot . "/local/blackboxsurvey/class/user.php");
/* Tao menu cho plugin */
function blackboxsurvey_extends_navigation(global_navigation $navigation) {
    
	$nodeBBSurvey 		= $navigation->add(get_string('blackbox_survey', 'local_blackboxsurvey'));//add node cha trong menu
    
    //mac dinh là khong hien menu
    $admin_menu = false;
    $lecturer_menu = false;
    $matcher_menu = false;
    //hien het menu cua blackbx survey cho admin
	if(has_capability('local/blackboxsurvey:adminmanagement', context_system::instance())){
	    $nodeBBSurvey->forceopen = true;
        $admin_menu = true;
        $lecturer_menu = true;
        $matcher_menu = true;
        $s_match = 1;
	}
    //kiem tra quyen neu user khong phai admin
    else
    {
        try{
            global $USER;
            $c_user = new b_user();
            //kiem tra lecturer
            $check_lec = $c_user->check_lecturer_by_userid($USER->id);
            if($check_lec > 0)
                $lecturer_menu = true;
            //kiem tra matchmaker
            $check_mat = $c_user->check_matcher_by_userid($USER->id);
            if($check_mat > 0)
            {
                $matcher_menu = true;
                $s_match = 0;    
            }
        }catch(Exception $ex){}
    }
    
    //hien menu tuong ung voi role
    if($admin_menu)
    {
        $nodeCurr		= $nodeBBSurvey->add(get_string('admin_manage', 'local_blackboxsurvey'));
        		$nodeCreateSurvey		= $nodeCurr->add(get_string('create_survey1', 'local_blackboxsurvey'), new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/admin/create_survey.php'));
        		//$nodeSurveyManagement 		= $nodeCurr->add('Manage Survey', new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/admin/manage_survey.php'));
                $nodeSurveyManagement 		= $nodeCurr->add(get_string('survey_manage', 'local_blackboxsurvey'), new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/admin/manage_survey.php'));
    	$nodeQuestionManagement = $nodeCurr->add(get_string('question_manage', 'local_blackboxsurvey'));
                $nodeQuestionList = $nodeQuestionManagement->add(get_string('question_list', 'local_blackboxsurvey'), new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/admin/questionlist.php'));
        		$nodeQuestionAddition = $nodeQuestionManagement->add(get_string('add_question', 'local_blackboxsurvey'), new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/admin/add_single_question.php'));
                //$nodeUseryManagement 		= $nodeCurr->add('Manage Blackbox User', new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/admin/manage_user.php'));
    }
    if($lecturer_menu)
    {
        $nodeLecturer		= $nodeBBSurvey->add(get_string('manage_questionnaire', 'local_blackboxsurvey'), new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/lecturer/manage_questionaire.php'));
    }
    if($matcher_menu)
    {
        $nodematcher		= $nodeBBSurvey->add(get_string('manage_match', 'local_blackboxsurvey'), new moodle_url($CFG->wwwroot.'/local/blackboxsurvey/matcher/manage_bbsurvey.php?s='.$s_match));
    }
    // update csdl neu dang nhap thanh cong
    if($admin_menu || $lecturer_menu || $matcher_menu)
    {
        /*global $DB;
        $DB->execute('UPDATE mdl_b_blackbox_survey SET status = 1
                        WHERE date_start < DATE_ADD(now(), INTERVAL -30 DAY)
                        AND status = 0');
        $DB->execute('UPDATE mdl_b_blackbox_survey SET status = -1
                        WHERE date_end < DATE_ADD(now(), INTERVAL -30 DAY)
                        AND status = 1');*/
        
    }
	$nodeCurr->forceopen = true;
    $nodeBBSurvey->forceopen = true;
}
?>
